package com.yoj.common.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回类
 * @param <T>
 */
@Data
public class BaseResponse<T> implements Serializable {

    private Integer code;

    private T data;

    private String message;

    private BaseResponse() {
    }

    public BaseResponse(ResultCodeEnum resultCodeEnum) {
        this(resultCodeEnum.getCode(), null, resultCodeEnum.getMessage());
    }

    public BaseResponse(Integer code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public static <T> BaseResponse<T> build(Integer code, T data, String msg) {
        return new BaseResponse<>(code, data, msg);
    }

    /**
     * 成功
     */
    public static <T> BaseResponse<T> success(T data) {
        return BaseResponse.build(0, data, "ok");
    }

    /**
     * 成功
     */
    public static <T> BaseResponse<T> success(T data, String msg) {
        return BaseResponse.build(0, data, msg);
    }

    /**
     * 失败
     */
    public static <T> BaseResponse<T> error(ResultCodeEnum resultCodeEnum) {
        return BaseResponse.build(resultCodeEnum.getCode(), null, resultCodeEnum.getMessage());
    }

    /**
     * 失败
     */
    public static <T> BaseResponse<T> error(ResultCodeEnum resultCodeEnum, String msg) {
        return BaseResponse.build(resultCodeEnum.getCode(), null, msg);
    }

    /**
     * 失败
     */
    public static <T> BaseResponse<T> error(T data, ResultCodeEnum resultCodeEnum) {
        return BaseResponse.build(resultCodeEnum.getCode(), data, resultCodeEnum.getMessage());
    }

}
